package net.toocruel.iqismart.web.controller.common;

import net.toocruel.iqismart.dao.AreaRepository;
import net.toocruel.iqismart.entity.common.Area;
import net.toocruel.iqismart.service.helper.MessageHelp;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/6 上午4:38
 */
@Controller
@RequestMapping("")
public class CommonController extends AbstractBaseController {

    @Autowired
    private AreaRepository areaRepository;

    @RequestMapping("/sessionInvalid")
    public String sessionInvalid(HttpServletRequest request, HttpServletResponse response){
//        throw new SessionInvalidException("");
        return "redirect:"+request.getRequestURI();
    }

    @RequestMapping("common/loadCities/{id}")
    @ResponseBody
    public String loadCities(@PathVariable("id") Long id){
        Area parent = areaRepository.findById(id);
        List<Area> childs = parent.getChilds();
        StringBuilder sb = new StringBuilder();
        childs.stream().forEach(area -> {
            sb.append("<option value=\""+area.getId()+"\">"+area.getName()+"</option>");
        });
        return sb.toString();
    }

    @RequestMapping("common/cleanSessionMessage")
    @ResponseBody
    public String cleanSessionMessage(){
        MessageHelp.cleanSessionMessage();
        return "ok";
    }


}
